package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.PermissionMapper;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 根据id删除权限
     *
     * @param id   权限id
     * @param name 角色名称
     */
    @Override
    public void deleteById(Integer id, String name) {
        permissionMapper.deleteById(id, name);
    }

    /**
     * 添加/修改权限
     *
     * @param name
     * @param permissionIds
     */
    @Override
    public void edit(String name, Integer[] permissionIds) {
        permissionMapper.delete(name);//先删除
        permissionMapper.edit(name, permissionIds);//再添加
    }

    /**
     * 通过用户名查询权限id
     *
     * @param name
     * @return
     */
    @Override
    public List<Integer> findPermissionIdsByRoleName(String name) {
        return permissionMapper.findPermissionIdsByRoleName(name);
    }

    /**
     * 查询权限列表全部数据
     *
     * @return
     */
    @Override
    public List<Permission> findAllPermission() {
        return permissionMapper.findAllPermission();
    }

    /**
     * 查询权限列表分页数据
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString, String name) {
        //定义页面大小和当前页的页码
        PageHelper.startPage(currentPage, pageSize);

        //条件查询数据
        List<Permission> permissionList = permissionMapper.findByCondition(queryString,name);

        //将数据进行包装
        PageInfo pageInfo = new PageInfo<Permission>(permissionList);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }
}
